package net.xi.dmhome.controller;

import cn.hutool.core.map.MapUtil;
import net.xi.dmhome.Utils.OSSUtil;
import net.xi.dmhome.entity.User;
import net.xi.dmhome.handler.Result;
import net.xi.dmhome.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @Author: jxhu
 * @Description:文件处理
 * @Date Created in 2021-02-20 20:04
 * @Modified By:
 */
@RestController
public class FileController {
    @Autowired
    UserMapper userMapper;


    SimpleDateFormat sdf = new SimpleDateFormat("/yyyy/MM/dd/");


    @PostMapping("/upload/{uuid}")
    public Result fileupload(@PathVariable("uuid") String uuid, MultipartFile file, HttpServletRequest request) throws FileNotFoundException {

        /*

        String path = "D:/tmp/img/";
        String originName = file.getOriginalFilename();
       *//* if (!originName.endsWith(".png")||!originName.endsWith(".jpg")
                ||!originName.endsWith(".jpeg")
        ){
            return Result.fail("文件类型不对");
        }*//*
        String format = sdf.format(new Date());
        String realPath = path + format;
        File folder = new File(realPath);
        if (!folder.exists()){
            folder.mkdirs();
        }
        String newName = UUID.randomUUID().toString() + ".jpg";*/
        User user = userMapper.selectByUserUUid(uuid);
        OSSUtil ossClient = new OSSUtil();
        String url = "";
        if (!((MultipartFile) file).isEmpty()) {
            try {
                //将文件上传
                String name = ossClient.uploadImg2Oss(file);
                //获取文件的URl地址  以便前台  显示
                String imgUrl = ossClient.getImgUrl(name);
                String originName = file.getOriginalFilename();
                url = imgUrl;
                System.out.println(url);
                user.setAvatar(url);
                userMapper.updateById(user);
                return Result.success(MapUtil.builder()
                        .put("url", url)
                        .map()
                );
            } catch (IOException e) {
                e.printStackTrace();
                return Result.fail(e.getMessage());
            }
        } else {
            return Result.fail("上传文件失败");
        }
        /*try {
            file.transferTo(new File(folder,newName));
            String url = request.getScheme() + "://" +request.getServerName()+":"+request.getServerPort()+format+newName;

        } catch (IOException e) {
            e.printStackTrace();

        }*/
    }

    @PostMapping("/uploadimg")
    public Result imgupload(MultipartFile file) {

     /*   String path = "D:/tmp/img/";
        String originName = file.getOriginalFilename();
       *//* if (!originName.endsWith(".png")||!originName.endsWith(".jpg")
                ||!originName.endsWith(".jpeg")
        ){
            return Result.fail("文件类型不对");
        }*//*
        String format = sdf.format(new Date());
        String realPath = path + format;
        File folder = new File(realPath);
        if (!folder.exists()){
            folder.mkdirs();
        }
        String newName = UUID.randomUUID().toString() + ".jpg";
        try {
            file.transferTo(new File(folder,newName));
            String url = request.getScheme() + "://" +request.getServerName()+":"+request.getServerPort()+format+newName;
            System.out.println(url);
            return Result.success(url);
        } catch (IOException e) {
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
    }*/
        OSSUtil ossClient = new OSSUtil();
        if (!((MultipartFile) file).isEmpty()) {
            try {
                //将文件上传
                String name = ossClient.uploadImg2Oss(file);
                //获取文件的URl地址  以便前台  显示
                String imgUrl = ossClient.getImgUrl(name);
                String originName = file.getOriginalFilename();

                System.out.println(imgUrl);
                return Result.success(imgUrl);
            } catch (IOException e) {
                e.printStackTrace();
                return Result.fail(e.getMessage());
            }
        } else {
            return Result.fail("上传失败");
        }
    }

}